package com.qfedu.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qfedu.common.JsonResult;
import com.qfedu.entity.Course;
import com.qfedu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: backStage
 * author: CGY
 * time: 2020/9/30 9:11
 * description:学科管理控制类
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 新增学科功能
     * @param name
     * @param createtime
     * @param week
     * @return
     */
    @RequestMapping("/addCourse.do")
    @ResponseBody
    public JsonResult addCourse(String name, Date createtime,Integer week){
        System.out.println("???" + name + createtime + week);
        Course course = new Course();
        course.setName(name);
        course.setCreatetime(createtime);
        course.setWeek(week);
        System.out.println("course = " + course);

        courseService.addCourse(course);
        return new JsonResult(1,null);
    }

    /**
     * 分页功能的实现
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/coursepage.do")
    @ResponseBody
    public Map<String,Object> selectAll(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);

        List<Course> list = courseService.selectAll();
        long total = ((Page)list).getTotal();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", total);
        map.put("data", list);
        return map;
    }

    /**
     * 学科修改功能
     * @param id
     * @param name
     * @param createtime
     * @param week
     * @return
     */
    @RequestMapping("/courseupdate.do")
    @ResponseBody
    public JsonResult update(Integer id,String name,Date createtime,Integer week){
        System.out.println("??" + id + name + createtime + week);
        Course course = new Course();
        course.setId(id);
        course.setName(name);
        course.setCreatetime(createtime);
        course.setWeek(week);
        courseService.update(course);
        return new JsonResult(1,"OK");
    }

    @RequestMapping("/coursedelete.do")
    @ResponseBody
    public JsonResult dalete(Integer id){
        courseService.delete(id);
        return new JsonResult(1,"删除成功");
    }
}
